package com.xbongbong.paas.service.impl;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.pojo.CallBatchCallConsumePojo;
import com.xbongbong.paas.pojo.CallBatchCallRechargePojo;
import com.xbongbong.paas.pojo.CallTaskListPojo;
import com.xbongbong.paas.pojo.CallTaskLogCountPojo;
import com.xbongbong.paas.pojo.dto.CallBatchCallConsumeDTO;
import com.xbongbong.paas.pojo.dto.CallBatchCallRechargeDTO;
import com.xbongbong.paas.pojo.dto.CallCenterAccountUserInfoDTO;
import com.xbongbong.paas.pojo.dto.CallGetNotEndCallDTO;
import com.xbongbong.paas.pojo.dto.CallTaskAgainDTO;
import com.xbongbong.paas.pojo.dto.CallTaskListDTO;
import com.xbongbong.paas.pojo.dto.CallTaskLogListDTO;
import com.xbongbong.paas.pojo.dto.CallTaskSaveDTO;
import com.xbongbong.paas.pojo.dto.FeeRemainDTO;
import com.xbongbong.paas.pojo.vo.CallBatchCallConsumeVO;
import com.xbongbong.paas.pojo.vo.CallBatchCallRechargeVO;
import com.xbongbong.paas.pojo.vo.CallCenterAccountUserInfoVO;
import com.xbongbong.paas.pojo.vo.CallGetNotEndCallVO;
import com.xbongbong.paas.pojo.vo.CallTaskAgainVO;
import com.xbongbong.paas.pojo.vo.CallTaskListVO;
import com.xbongbong.paas.pojo.vo.CallTaskLogCountListVO;
import com.xbongbong.paas.pojo.vo.CallTaskLogCountVO;
import com.xbongbong.paas.pojo.vo.CallTaskLogVO;
import com.xbongbong.paas.pojo.vo.CallTaskSaveVO;
import com.xbongbong.paas.pojo.vo.FeeRemainVO;
import com.xbongbong.paas.service.CallCenterAccountService;
import com.xbongbong.paas.service.XbbCallTaskLogService;
import com.xbongbong.paas.service.XbbCallTaskService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.helper.PageHelper;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.PageHelperUtil;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.CallCenterErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.domain.entity.CallTaskEntity;
import com.xbongbong.saas.domain.entity.CallTaskLogEntity;
import com.xbongbong.saas.model.XbbCallTaskLogModel;
import com.xbongbong.saas.model.XbbCallTaskModel;
import com.xbongbong.sys.domain.entity.FeeLogEntity;
import com.xbongbong.sys.domain.entity.UserEntity;
import com.xbongbong.sys.model.FeeLogModel;
import com.xbongbong.sys.model.UserModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author youli.chen
 * @version v1.0
 * @description: TODO
 * @date 2019/2/22 19:26
 * @since v1.0
 */
@Service("xbbCallTaskService")
public class XbbCallTaskServiceImpl implements XbbCallTaskService {

    private static final Logger LOG = LoggerFactory.getLogger(XbbCallTaskServiceImpl.class);

    @Resource
    private UserModel userModel;
    @Resource
    private FeeLogModel feeLogModel;
    @Resource
    private XbbCallTaskModel xbbCallTaskModel;
    @Resource
    private XbbCallTaskLogModel xbbCallTaskLogModel;
    @Resource
    private XbbCallTaskLogService xbbCallTaskLogService;
    @Resource
    private CallCenterAccountService callCenterAccountService;

    /**
     * 保存批量外呼计划
     *
     * @param callTaskSaveDTO 保存批量外呼计划入参
     * @return CallTaskSaveVO 保存批量外呼计划回参
     * @throws XbbException 销帮帮设定的异常
     * @author youli.chen
     * @version v1.0
     * @since v1.0
     */
    @Override
    public XbbResponse<CallTaskSaveVO> callTaskSave(CallTaskSaveDTO callTaskSaveDTO) throws XbbException {
        CallTaskSaveVO callTaskSaveVO = new CallTaskSaveVO();
        CallTaskEntity callTaskEntity = new CallTaskEntity();
        BeanUtils.copyProperties(callTaskSaveDTO, callTaskEntity);
        callTaskEntity.setCallTaskConfig(JSON.toJSONString(callTaskSaveDTO.getCallTaskConfig()));
        Integer saveResult = xbbCallTaskModel.insert(callTaskEntity);
        if (saveResult > 0) {
            // 列表批量外呼日志保存
            xbbCallTaskLogService.batchSave(callTaskSaveDTO.getCorpid(), callTaskEntity.getId(), callTaskSaveDTO);
            callTaskSaveVO.setCallTaskId(callTaskEntity.getId());
        } else {
            throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301084);
        }
        return new XbbResponse<>(callTaskSaveVO);
    }

    /**
     * 批量外呼计划列表
     *
     * @param callTaskListDTO 批量外呼计划列表入参
     * @return CallTaskListVO 批量外呼计划列表回参
     * @throws XbbException 销帮帮设定的异常
     * @author youli.chen
     * @version v1.0
     * @since v1.0
     */
    @Override
    public XbbResponse<CallTaskListVO> callTaskList(CallTaskListDTO callTaskListDTO) throws XbbException {
        CallTaskListVO callTaskListVO;
        // 登录用户
        UserEntity logUser = userModel.getByKey(callTaskListDTO.getUserId(), callTaskListDTO.getCorpid());
        if (logUser == null) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100004);
        }
        boolean isBoss = userModel.isBoss(logUser);
        CallCenterAccountUserInfoDTO userInfoDTO = new CallCenterAccountUserInfoDTO();
        BeanUtils.copyProperties(callTaskListDTO, userInfoDTO);
        if (isBoss) {
            // 老板、管理员等角色不走话务中心的权限判断
            callTaskListVO = getAdminCallTaskList(callTaskListDTO);
        } else {
            CallCenterAccountUserInfoVO userInfoVO = callCenterAccountService.getByXbbUserInfo(userInfoDTO);
            if (userInfoVO == null) {
                throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301003);
            } else if (userInfoVO.getIsAdmin() == 1) {
                // 超管能看到所有人的批量外呼记录
                callTaskListVO = getAdminCallTaskList(callTaskListDTO);
            } else if (userInfoVO.getIsSupervisor() == 1) {
                // 部门主管能看到全部门的批量外呼记录
                if (callTaskListDTO.getDepId() != null && callTaskListDTO.getDepId() > 0L) {
                    if (!Objects.equals(callTaskListDTO.getDepId(), userInfoVO.getDepId())) {
                        throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301015);
                    }
                } else if (callTaskListDTO.getDepId() == null && callTaskListDTO.getAgentUserId() == null) {
                    // 当前端没有传depId时，这个主管权限有问题
                    callTaskListDTO.setDepId(userInfoVO.getDepId());
                }
                callTaskListVO = getSupervisorCallTaskList(callTaskListDTO);
            } else if (userInfoVO.getIsAgent() == 1) {
                // 坐席能看到自己的批量外呼记录
                callTaskListVO = getAgentCallTaskList(callTaskListDTO);
            } else {
                throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301002);
            }
        }
        return new XbbResponse<>(callTaskListVO);
    }

    /**
     * 查看上次是否有未完成的批量外呼进度
     *
     * @param callGetNotEndCallDTO 查看上次是否有未完成的批量外呼进度入参
     * @return CallGetNotEndCallVO 查看上次是否有未完成的批量外呼进度回参
     * @throws XbbException 销帮帮设定的异常
     * @author youli.chen
     * @version v1.0
     * @since v1.0
     */
    @Override
    public XbbResponse<CallGetNotEndCallVO> getNotEndCall(CallGetNotEndCallDTO callGetNotEndCallDTO) throws XbbException {
        CallGetNotEndCallVO callGetNotEndCallVO = new CallGetNotEndCallVO();
        String corpid = callGetNotEndCallDTO.getCorpid();
        Map<String, Object> params = new HashMap<>(16);
        params.put("corpid", corpid);
        params.put("userId", callGetNotEndCallDTO.getUserId());
        params.put("del", DelEnum.NORMAL.getDel());
        params.put("orderByStr", "add_time DESC");
        List<CallTaskEntity> callTaskList = xbbCallTaskModel.findEntitys(params);
        CallTaskEntity callTaskEntity = callTaskList.size() > 0 ? callTaskList.get(0) : null;

        if (callTaskEntity == null) {
            throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301085);
        } else {
            callGetNotEndCallVO.setCorpid(callTaskEntity.getCorpid());
            callGetNotEndCallVO.setUserId(callTaskEntity.getUserId());
            callGetNotEndCallVO.setCallTaskId(callTaskEntity.getId());

            Map<String, Object> paramsLog = new HashMap<>(16);
            paramsLog.put("corpid", corpid);
            paramsLog.put("callTaskId", callTaskEntity.getId());
            paramsLog.put("del", DelEnum.NORMAL.getDel());
            List<CallTaskLogEntity> taskLogEntList = xbbCallTaskLogModel.findEntitys(paramsLog);
            List<Long> cusId = new ArrayList<>();
            for (CallTaskLogEntity taskLogEntity : taskLogEntList) {
                if(taskLogEntity.getCustomerId() != null && taskLogEntity.getContactId() != null){
                    // 联系人数量
                    cusId.add(taskLogEntity.getContactId());
                }else {
                    // 客户数量
                    cusId.add(taskLogEntity.getCustomerId());
                }
            }
            //去重
            LinkedHashSet<Long> set = new LinkedHashSet<>(cusId);
            set.addAll(cusId);
            cusId.clear();
            cusId.addAll(set);
            //客户总数量
            callGetNotEndCallVO.setCallTaskCusCount(cusId.size());

            paramsLog.put("phoneStatus", "xbbCall");
            Integer taskLogTotal = xbbCallTaskLogModel.getEntitysCount(paramsLog);
            //未拨打的客户数量
            callGetNotEndCallVO.setCallTaskNotUseCusCount(taskLogTotal);
            //已经拨打的客户数量
            callGetNotEndCallVO.setCallTaskUseCusCount(cusId.size() - taskLogTotal);
        }
        return new XbbResponse<>(callGetNotEndCallVO);
    }

    /**
     * 批量外呼消费明细列表
     *
     * @param callBatchCallConsumeDTO 批量外呼消费明细列表入参
     * @return CallBatchCallConsumeVO 批量外呼消费明细列表回参
     * @throws XbbException 销帮帮设定的异常
     * @author youli.chen
     * @version v1.0
     * @since v1.0
     */
    @Override
    public XbbResponse<CallBatchCallConsumeVO> batchCallConsume(CallBatchCallConsumeDTO callBatchCallConsumeDTO) throws XbbException {
        FeeRemainDTO feeRemainDTO = new FeeRemainDTO();
        feeRemainDTO.setCorpid(callBatchCallConsumeDTO.getCorpid());
        FeeRemainVO feeRemainVO = xbbCallTaskLogService.getRemain(feeRemainDTO);
        if (feeRemainVO == null) {
            throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301045);
        }

        CallBatchCallConsumeVO callBatchCallConsumeVO = new CallBatchCallConsumeVO();
        Map<String, Object> params = new HashMap<>(16);
        params.put("corpid", callBatchCallConsumeDTO.getCorpid());
        params.put("status", 1);
        params.put("del", DelEnum.NORMAL.getDel());
        try {
            PageHelper pageHelper = new PageHelper(callBatchCallConsumeDTO.getPage(), callBatchCallConsumeDTO.getPageSize());
            List<CallTaskEntity> callTaskList = (List<CallTaskEntity>) PageHelperUtil.getEntityList(params, pageHelper, xbbCallTaskModel);

            List<CallBatchCallConsumePojo> consumePojoList = new ArrayList<>();
            if (callTaskList != null) {
                Set<String> userIdSet = new HashSet<>();
                for (CallTaskEntity it : callTaskList) {
                    CallBatchCallConsumePojo consumePojo = new CallBatchCallConsumePojo();
                    BeanUtil.copyProperties(it, consumePojo);
                    userIdSet.add(it.getUserId());
                    if (it.getTaskFactNum() != null) {
                        consumePojo.setConsumeCount("-" + it.getTaskFactNum().toString());
                    } else {
                        consumePojo.setConsumeCount("0");
                    }

                    consumePojo.setRemain(feeRemainVO.getGetRemain());
                    consumePojo.setConsumeTime(DateUtil.getString(it.getUpdateTime(), DateUtil.SDF));
                    consumePojoList.add(consumePojo);
                }
                // 封装前端显示的坐席名称
                List<String> userIds = new ArrayList<>(userIdSet);
                Map<String, String> userIdNameMap = userModel.getUserIdNameMap(callBatchCallConsumeDTO.getCorpid(), userIds);
                for (CallBatchCallConsumePojo consume : consumePojoList) {
                    consume.setUserName(userIdNameMap.get(consume.getUserId()));
                }
            }
            callBatchCallConsumeVO.setCallTaskList(consumePojoList);
            callBatchCallConsumeVO.setPageHelper(pageHelper);
        } catch (Exception e) {
            LOG.error("XbbCallTaskServiceImpl.batchCallConsume 执行出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return new XbbResponse<>(callBatchCallConsumeVO);
    }

    /**
     * 批量外呼充值明细列表
     *
     * @param callBatchCallRechargeDTO 批量外呼充值明细列表入参
     * @return CallBatchCallRechargeVO 批量外呼充值明细列表回参
     * @throws XbbException 销帮帮设定的异常
     * @author youli.chen
     * @version v1.0
     * @since v1.0
     */
    @Override
    public XbbResponse<CallBatchCallRechargeVO> batchCallRecharge(CallBatchCallRechargeDTO callBatchCallRechargeDTO) throws XbbException {
        CallBatchCallRechargeVO rechargeVO = new CallBatchCallRechargeVO();
        String corpid = callBatchCallRechargeDTO.getCorpid();
        FeeRemainDTO feeRemainDTO = new FeeRemainDTO();
        feeRemainDTO.setCorpid(corpid);
        FeeRemainVO feeRemainVO = xbbCallTaskLogService.getRemain(feeRemainDTO);
        if (feeRemainVO == null) {
            throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301045);
        }

        Map<String, Object> param = new HashMap<>(16);
        param.put("del", DelEnum.NORMAL.getDel());
        // 支付完成
        param.put("isPay", 1);
        // 批量外呼套餐
        param.put("feeType", 3);
        param.put("corpid", corpid);
        param.put("orderByStr", "update_time DESC");
        try {
            PageHelper pageHelper = new PageHelper(callBatchCallRechargeDTO.getPage(), callBatchCallRechargeDTO.getPageSize());
            List<FeeLogEntity> feeLogEntityList = (List<FeeLogEntity>) PageHelperUtil.getEntityList(param, pageHelper, feeLogModel);
            List<CallBatchCallRechargePojo> callTaskList = new ArrayList<>();
            if (feeLogEntityList != null) {
                HashMap<Long, BigDecimal> feeIdAndFeeCount = feeRemainVO.getFeeIdAndFeeCount();
                BigDecimal remain = feeRemainVO.getGetRemain();
                for (FeeLogEntity feeLogEntity : feeLogEntityList) {
                    CallBatchCallRechargePojo rechargePojo = new CallBatchCallRechargePojo();
                    BeanUtil.copyProperties(feeLogEntity, rechargePojo);
                    rechargePojo.setRechargeTime(DateUtil.getString(feeLogEntity.getUpdateTime(), DateUtil.SDF));
                    rechargePojo.setRemain(remain.toString());
                    String rechargeCount = feeIdAndFeeCount.get(feeLogEntity.getFeeId()) == null ?
                            BigDecimal.ZERO.toString() : feeIdAndFeeCount.get(feeLogEntity.getFeeId()).toString();
                    rechargePojo.setRechargeCount("+" + rechargeCount);
                    callTaskList.add(rechargePojo);
                }
                rechargeVO.setCallTaskList(callTaskList);
                rechargeVO.setPageHelper(pageHelper);
            }
        } catch (Exception e) {
            LOG.error("XbbCallTaskServiceImpl.batchCallRecharge 执行出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return new XbbResponse<>(rechargeVO);
    }

    /**
     * 再次智能批量外呼计划
     *
     * @param callTaskAgainDTO 再次智能批量外呼计划入参
     * @return CallTaskAgainVO 再次智能批量外呼计划回参
     * @throws XbbException 销帮帮设定的异常
     * @author youli.chen
     * @version v1.0
     * @since v1.0
     */
    @Override
    public XbbResponse<CallTaskAgainVO> callTaskAgain(CallTaskAgainDTO callTaskAgainDTO) throws XbbException {
        CallTaskAgainVO callTaskAgainVO = new CallTaskAgainVO();
        String corpid = callTaskAgainDTO.getCorpid();
        Long callTaskId = callTaskAgainDTO.getCallTaskId();
        CallTaskEntity callTaskEntity;
        callTaskEntity = xbbCallTaskModel.getDetailById(callTaskId, corpid);
        callTaskEntity.setId(null);
        Integer saveResult = xbbCallTaskModel.insert(callTaskEntity);
        if (saveResult > 0) {
            CallTaskSaveDTO callTaskSaveDTO = callTaskAddAgain(callTaskId, corpid,
                    callTaskAgainDTO.getPhoneList(), callTaskAgainDTO.getTypeList());
            //列表批量外呼日志保存
            xbbCallTaskLogService.batchSave(corpid, callTaskEntity.getId(), callTaskSaveDTO);
            callTaskAgainVO.setCallTaskId(callTaskEntity.getId());
        } else {
            throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301084);
        }
        return new XbbResponse<>(callTaskAgainVO);
    }

    /**
     * 超管查找：根据业务对象封装的条件查找符合的列表
     *
     * @param callTaskListDTO 业务对象封装的条件
     * @return CallTaskListVO 符合条件的批量外呼列表
     * @author youli.chen
     */
    private CallTaskListVO getAdminCallTaskList(CallTaskListDTO callTaskListDTO) throws XbbException {
        Map<String, Object> params = new HashMap<>(16);
        if (callTaskListDTO.getDepId() != null && callTaskListDTO.getDepId() > 0L) {
            List<Long> depIdIn = new ArrayList<>();
            depIdIn.add(callTaskListDTO.getDepId());
            List<String> userIdIn = userModel.getUserIdsRecursionDep(depIdIn, callTaskListDTO.getCorpid(), 1);
            if (userIdIn != null && userIdIn.size() > 0) {
                params.put("userIdIn", userIdIn);
            } else {
                params.put("userId", callTaskListDTO.getAgentUserId());
            }
        } else {
            params.put("userId", callTaskListDTO.getAgentUserId());
        }
        return getCallTaskList(params, callTaskListDTO);
    }

    /**
     * 主管查找：根据业务对象封装的条件查找符合的列表
     *
     * @param callTaskListDTO 业务对象封装的条件
     * @return CallTaskListVO 符合条件的批量外呼列表
     * @author youli.chen
     */
    private CallTaskListVO getSupervisorCallTaskList(CallTaskListDTO callTaskListDTO) throws XbbException {
        Map<String, Object> params = new HashMap<>(16);
        // 输入当前主管的部门id查询部门下的所有员工
        List<Long> depIdIn = new ArrayList<>();
        depIdIn.add(callTaskListDTO.getDepId());
        List<String> userIdIn = userModel.getUserIdsRecursionDep(depIdIn, callTaskListDTO.getCorpid());
        if (userIdIn != null && userIdIn.size() > 0) {
            params.put("userIdIn", userIdIn);
        } else {
            params.put("userId", callTaskListDTO.getAgentUserId());
        }
        return getCallTaskList(params, callTaskListDTO);
    }

    /**
     * 坐席查找：根据业务对象封装的条件查找符合的列表
     *
     * @param callTaskListDTO 业务对象封装的条件
     * @return CallTaskListVO 符合条件的批量外呼列表
     * @author youli.chen
     */
    public CallTaskListVO getAgentCallTaskList(CallTaskListDTO callTaskListDTO) throws XbbException {
        Map<String, Object> params = new HashMap<>(16);
        params.put("userId", callTaskListDTO.getUserId());
        return getCallTaskList(params, callTaskListDTO);
    }

    /**
     * 获取批量外呼任务的基本参数封装
     *
     * @author youli.chen
     */
    private CallTaskListVO getCallTaskList(Map<String, Object> params, CallTaskListDTO callTaskListDTO) throws XbbException {
        CallTaskListVO callTaskListVO = new CallTaskListVO();
        params.put("del", DelEnum.NORMAL.getDel());
        // 排序因为有注入问题不能通过外部传入
        params.put("orderByStr", "add_time DESC");

        PageHelper pageHelper = new PageHelper(callTaskListDTO.getPage(), callTaskListDTO.getPageSize());
        List<CallTaskEntity> callTaskList = (List<CallTaskEntity>) PageHelperUtil.getEntityList(params, pageHelper, xbbCallTaskModel);

        List<CallTaskListPojo> callTaskListPojos = new ArrayList<>();
        if (callTaskList != null) {
            List<Long> callTaskIds = new ArrayList<>();
            for (CallTaskEntity callTaskEntity : callTaskList) {
                callTaskIds.add(callTaskEntity.getId());
            }
            LinkedHashMap<Long, CallTaskLogCountVO> countVO;
            CallTaskLogCountListVO countListVO = xbbCallTaskLogService.countStatusListByTaskIds(callTaskListDTO.getCorpid(), callTaskIds);

            Set<String> userIdSet = new HashSet<>();
            for (CallTaskEntity callTaskEntity : callTaskList) {
                CallTaskListPojo callTaskListPojo = new CallTaskListPojo();
                BeanUtil.copyProperties(callTaskEntity, callTaskListPojo);

                CallTaskLogCountVO taskLogCountVO = new CallTaskLogCountVO();
                for (CallTaskLogCountPojo pojo : countListVO.getCallTaskLogCountListVO()) {
                    countVO = pojo.getTaskLogCountList();
                    taskLogCountVO = countVO.get(callTaskEntity.getId());
                }
                if(taskLogCountVO == null){
                    continue;
                }
                // 各个通话状态统计赋值
                BeanUtil.copyProperties(taskLogCountVO, callTaskListPojo);

                userIdSet.add(callTaskListPojo.getUserId());

                String addTimeStr = DateUtil.getString(callTaskEntity.getAddTime(), new SimpleDateFormat("yyyy-MM-dd HH:mm"));
                callTaskListPojo.setAddTime(addTimeStr);
                callTaskListPojos.add(callTaskListPojo);
            }
            // 封装前端显示的坐席名称
            List<String> userIds = new ArrayList<>(userIdSet);
            Map<String, String> userIdNameMap = userModel.getUserIdNameMap(callTaskListDTO.getCorpid(), userIds);
            for (CallTaskListPojo it : callTaskListPojos) {
                it.setUserName(userIdNameMap.get(it.getUserId()));
            }
        }

        callTaskListVO.setCallTaskList(callTaskListPojos);
        callTaskListVO.setPageHelper(pageHelper);
        return callTaskListVO;
    }

    /**
     * 再次开启批量外呼
     *
     * @param callTaskId   批量外呼任务主键
     * @param corpid       公司ID
     * @param phoneList    客户状态筛选
     * @param customerList 通话结果筛选
     * @return CallTaskSaveDTO 符合条件的批量外呼
     * @author youli.chen
     */
    private CallTaskSaveDTO callTaskAddAgain(Long callTaskId, String corpid, List<Integer> phoneList,
                                             List<Integer> customerList) throws XbbException {
        CallTaskSaveDTO callTaskSaveDTO = new CallTaskSaveDTO();
        CallTaskLogListDTO callTaskLogListDTO = new CallTaskLogListDTO();
        callTaskLogListDTO.setCallTaskId(callTaskId);
        callTaskLogListDTO.setCorpid(corpid);
        callTaskLogListDTO.setStatusIn(phoneList);
        callTaskLogListDTO.setTypeIn(customerList);

        CallTaskLogVO callTaskLogVO = xbbCallTaskLogService.findDetails(callTaskLogListDTO);
        if (callTaskLogVO.getCallTaskLogList() == null) {
            throw new XbbException(CallCenterErrorCodeEnum.API_ERROR_301086);
        }
        List<Long> customerIdList = new ArrayList<>();
        List<Long> contactIdList = new ArrayList<>();
        for (CallTaskLogEntity taskLogEntity : callTaskLogVO.getCallTaskLogList()) {
            // 联系人
            if (taskLogEntity.getContactId() != null && taskLogEntity.getCustomerId() != null) {
                contactIdList.add(taskLogEntity.getContactId());
            }
            // 客户
            if (taskLogEntity.getCustomerId() != null && taskLogEntity.getContactId() == null) {
                customerIdList.add(taskLogEntity.getCustomerId());
            }
        }
        callTaskSaveDTO.setContactIdList(contactIdList);
        callTaskSaveDTO.setCustomerIdList(customerIdList);
        return callTaskSaveDTO;
    }
}
